#import "BlueClassicPlusPlugin.h"
#import "ServerBlue.h"
#import "ClientBlue.h"

@interface BlueClassicPlusPlugin()<FlutterStreamHandler>

@property (nonatomic,strong) FlutterEventSink sink;
@property (nonatomic,strong) ServerBlue * serverBlue;
@property (nonatomic,strong) ClientBlue * clientBlue;

@end

@implementation BlueClassicPlusPlugin

#pragma mark 插件注册入口
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"blue_classic_plus"
            binaryMessenger:[registrar messenger]];
  BlueClassicPlusPlugin* instance = [[BlueClassicPlusPlugin alloc] init];
    [registrar addMethodCallDelegate:instance channel:channel];
    
    FlutterEventChannel * event = [FlutterEventChannel eventChannelWithName:@"blue_classic_plus_eventChannel" binaryMessenger:registrar.messenger];
    [event setStreamHandler:instance];
}

- (FlutterError* _Nullable)onListenWithArguments:(id _Nullable)arguments
                                       eventSink:(FlutterEventSink)events{
    
    self.sink = events;
    return nil;
}
- (FlutterError* _Nullable)onCancelWithArguments:(id _Nullable)arguments{
    return nil;
}


#pragma mark 监听Flutter调用原生代码入口
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    if ([@"getPlatformVersion" isEqualToString:call.method]) {
        result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
        
    }else if ([@"bluetoothStartServer" isEqualToString:call.method]) {
        if (self.serverBlue.serverListening) {
            NSLog(@"不需要重复开启");
            return;
        }
        
        ///打开蓝牙服务端
        NSString * uuid = call.arguments[@"uuid"];
        NSString * name = call.arguments[@"name"];
        self.serverBlue = [[ServerBlue alloc] initWithSink:self.sink uuid:uuid name:name];
        
    } else if ([@"bluetoothServerBroadcastMsg" isEqualToString:call.method]) {
        ///服务端广播消息
        NSString * msg = call.arguments[@"msg"];
        [self.serverBlue broadcastData:[msg dataUsingEncoding:NSUTF8StringEncoding]];
        
    } else if ([@"bluetoothStopServer" isEqualToString:call.method]) {
        ///关闭蓝牙服务端
        [self.serverBlue stopServer];
        result([NSNumber numberWithBool:YES]);
    } else if ([@"bluetoothStartDiscovery" isEqualToString:call.method]) {
        
        if (self.clientBlue.isConnecting) {
            NSLog(@"不用重复连接");
            return;
        }
        
        NSString * uuid = call.arguments[@"uuid"];
        NSString * name = call.arguments[@"name"];
        
        ///扫描设备
        self.clientBlue = [[ClientBlue alloc] initWithSink:self.sink uuid:uuid name:name];
        [self.clientBlue startScan:nil];
    } else if ([@"bluetoothEndDiscovery" isEqualToString:call.method]) {
        ///结束扫描
        [self.clientBlue stopScan];
    } else if ([@"bluetoothStopClient" isEqualToString:call.method]) {
        ///结束连接
        [self.clientBlue bluetoothStopConnect];
    }
        
    else {
        result(FlutterMethodNotImplemented);
    }
}


@end
